package cmcc.resource;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashSet;

import cmcc.util.DB;

public class Resource {
	String mobile_number;
	String mobile_type;
	String card_number;
	String is_available;

	public String getMobile_number() {
		return mobile_number;
	}

	public void setMobile_number(String mobile_number) {
		this.mobile_number = mobile_number;
	}

	public String getMobile_type() {
		return mobile_type;
	}

	public void setMobile_type(String mobile_type) {
		this.mobile_type = mobile_type;
	}

	public String getCard_number() {
		return card_number;
	}

	public void setCard_number(String card_number) {
		this.card_number = card_number;
	}

	public String getIs_available() {
		return is_available;
	}

	public void setIs_available(String is_available) {
		this.is_available = is_available;
	}

	public static boolean addBeginToEnd(String start, String end,
			String num_type) {
		long long_start = Long.parseLong(start);
		long long_end = Long.parseLong(end);
		if (long_start > long_end) {
			long t;
			t = long_start;
			long_start = long_end;
			long_end = t;
		}
		Connection conn = DB.getConn();
		String sql = "insert into t_mobiles values(?,?,?," + "'Y'" + ")";
		PreparedStatement pstmt = DB.prepare(conn, sql);
		try {
			conn.setAutoCommit(false);

			for (long i = long_start; i <= long_end; i++) {

				Date d = new Date();
				long card_num = d.getTime();
				pstmt.setString(1, i + "");
				pstmt.setString(3, card_num + "");
				pstmt.setString(2, num_type);
				pstmt.executeUpdate();
				conn.commit();

				System.out.println(i + "\t" + num_type + "\t" + card_num);

			}
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public static boolean addBeginToEndBy(long long_start, long long_end,
			String num_type) {

		Connection conn = DB.getConn();
		String sql = "insert into t_mobiles values(?,?,?," + "'Y'" + ")";
		PreparedStatement pstmt = DB.prepare(conn, sql);
		try {
			conn.setAutoCommit(false);

			for (long i = long_start; i <= long_end; i++) {

				Date d = new Date();
				long card_num = d.getTime();
				pstmt.setString(1, i + "");
				pstmt.setString(3, card_num + "");
				pstmt.setString(2, num_type);
				pstmt.executeUpdate();
				conn.commit();

				System.out.println(i + "\t" + num_type + "\t" + card_num);

			}
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public static boolean addByFile(InputStream in) {
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String line;
		Connection conn = DB.getConn();
		String sql = "insert into t_mobiles values(?,?,?," + "'Y'" + ")";
		PreparedStatement pstmt = DB.prepare(conn, sql);
		
		try {
			conn.setAutoCommit(false);
			line = br.readLine();
			while (line != null) {
				System.out.println(line);
				String[] source = line.split(" |\t");			
				Date d = new Date();
				long card_num = d.getTime();
				pstmt.setString(1, source[1]+ "");
				pstmt.setString(3, card_num + "");
				pstmt.setString(2, source[0]);
				pstmt.executeUpdate();
				conn.commit();				
				line = br.readLine();
			}
			conn.setAutoCommit(true);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public static HashSet<String> getAvailableNnmber(int start, int wide){
		HashSet<String> nums = new HashSet<String>();
		Connection conn = DB.getConn();
		String sql = "SELECT * FROM t_mobiles WHERE is_available ='Y'"+"LIMIT " + start + "," + wide;;
		Statement stmt = DB.getStatement(conn);
		ResultSet rs= null;
		try {
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				String num = rs.getString("mobile_number");
				nums.add(num);
			}
		} catch (SQLException e) {
			e.printStackTrace();			 
		}finally {
			DB.close(conn);
			DB.close(rs);
			DB.close(stmt);
		}
		
		return nums;
	}

	public static int getCount() {
		int count = 0;
		Connection conn = DB.getConn();
		Statement stmt = DB.getStatement(conn);
		String sql = "SELECT COUNT(*) FROM t_mobiles";
		ResultSet rs = DB.getResultSet(stmt, sql);
		try {
			if (rs.next()) {
				count = rs.getInt(1);
				// System.out.println("Operator.java------- count = "+count);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(conn);
			DB.close(rs);
			DB.close(stmt);
		}
		return count;
	}
}
